package org.infodavid.common.impl.persistence;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.infodavid.common.IBuilder;
import org.infodavid.common.dto.IUserListItem;
import org.infodavid.common.model.IUser;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.persistence.PersonDataService;
import org.infodavid.common.persistence.UserDataService;
import org.infodavid.common.persistence.UserGroupDataService;

/**
 * The Class AbstractUserDataService.
 */
public abstract class AbstractUserDataService extends UserDataService {

	/** The Constant log. */
	private static final Log LOGGER = LogFactory.getLog(AbstractUserDataService.class);

	/** The builder. */
	private final IBuilder<IUser> builder = ModelFactory.getInstance().getBuilder(IUser.class);

	/**
	 * The Constructor.
	 */
	protected AbstractUserDataService() {
		super();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.persistence.IDataService#create()
	 */
	public IUser create() throws InstantiationException {
		return builder.create();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.persistence.IDataService#copyTo(org.infodavid.common.model.IDataObject,
	 * boolean, boolean)
	 */
	public IUser copyTo(final IUser from, final boolean deep, final boolean asNew)
	    throws PersistenceException, InstantiationException {
		final boolean isDebugEnabled = LOGGER.isDebugEnabled();

		if (isDebugEnabled) {
			LOGGER.debug("Copying entity from " + from + " using deep = " + deep + ", asNew = " + asNew);
		}

		final IUser to;

		if (from == null) {
			to = null;
		}
		else {
			to = builder.create();

			copyTo(from, to, asNew);
			to.setComment(from.getComment());
			to.setConnectionsCount(from.getConnectionsCount());

			if (deep) {
				to.setEmail(CopyUtil.copy(from.getEmail()));
			}
			else {
				to.setEmail(from.getEmail());
			}

			to.setExpirationDate(from.getExpirationDate());
			to.setLastConnectionDate(from.getLastConnectionDate());
			to.setLastIP(from.getLastIP());
			to.setLocked(from.isLocked());
			to.setName(from.getName());
			to.setPassword(from.getPassword());
			to.setTimeZone(from.getTimeZone());
			to.setPerson(PersonDataService.getInstance().copyTo(from.getPerson(), deep, asNew));
			to.setPseudonym(from.getPseudonym());
			to.setPrimaryGroup(UserGroupDataService.getInstance().copyTo(from.getPrimaryGroup(), deep,
			    asNew));

			if (from.getGroups() == null) {}
			else {
				to.getGroups().putAll(from.getGroups());
			}

			to.getProperties().getData().addAll(from.getProperties().getData());
		}

		if (isDebugEnabled) {
			LOGGER.debug("Copied entity = " + to);
		}

		return to;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.IDataService#copyListItemTo(org.infodavid.common.model.IDataObject
	 * , boolean, boolean)
	 */
	public IUserListItem copyListItemTo(final IUserListItem from, final boolean deep,
	    final boolean asNew) throws PersistenceException, InstantiationException {
		final boolean isDebugEnabled = LOGGER.isDebugEnabled();

		if (isDebugEnabled) {
			LOGGER.debug("Copying entity from " + from + " using deep = " + deep + ", asNew = " + asNew);
		}

		final IUser to;

		if (from == null) {
			to = null;
		}
		else {
			to = builder.create();

			copyTo(from, to, asNew);
			to.setName(from.getName());
			to.setPseudonym(from.getPseudonym());
		}

		if (isDebugEnabled) {
			LOGGER.debug("Copied entity = " + to);
		}

		return to;
	}
}
